package com.shiguiwu.springmybatis.javabase.generics;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.Arrays;
import java.util.List;

/**
 * @description: 泛型
 * @author: stone
 * @date: Created by 2021/3/12 11:28
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.generics
 */
public class GenericsMethodParameter<T> {

    public List<T> test(List<T> list) {
        return list;
    }

    public static void main(String[] args) throws NoSuchMethodException {
        Method test = GenericsMethodParameter.class.getDeclaredMethod("test",List.class);



        System.out.println("----------m1方法返回值类型信息------------");


        Type genericReturnType = test.getGenericReturnType();
        if (genericReturnType instanceof TypeVariable) {
            TypeVariable typeVariable = (TypeVariable) genericReturnType;
            System.out.println("变量名称：" + typeVariable.getName());
            System.out.println("这个变量在哪里声明：" + typeVariable.getGenericDeclaration());
            Type[] bounds = typeVariable.getBounds();
            System.out.println("上边界。。。。。。。。。。。。。。。。。");
            System.out.println(Arrays.toString(bounds));
        }

        System.out.println("方法上的参数类型列表============================================");
        //调用Method中的getGenericParameterTypes方法可以获取参数类型列表，包含了详细的泛型信息
        Type arg1Type   = test.getGenericParameterTypes()[0];

        if (arg1Type instanceof ParameterizedType) {//@3
            ParameterizedType parameterizedType = (ParameterizedType) arg1Type;
            System.out.println("原始类型：" + parameterizedType.getRawType());
            System.out.println("所属的类型:" + parameterizedType.getOwnerType());
            Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
            //泛型中第一个参数的类型是T，T是泛型变量，泛型变量对应java中的TypeVariable接口
            Type oneType = actualTypeArguments[0];//@4
            System.out.println("@5:" + oneType.getClass());//@5
            if (oneType instanceof TypeVariable) {
                System.out.println("这个参数是个泛型变量类型！");
                TypeVariable<Class<GenericsMethodParameter>> oneActualType = (TypeVariable) oneType;
                System.out.println("变量名称:" + oneActualType.getName());
                System.out.println("这个变量在哪声明的:" + oneActualType.getGenericDeclaration());
                Type[] bounds = oneActualType.getBounds();
                System.out.println("这个变量上边界数量:" + bounds.length);
                System.out.println("这个变量上边界清单:");
                for (Type bound : bounds) {
                    System.out.println(bound.getTypeName());
                }
            }
        }




    }
}
